//
//  TabBarView.m
//  LotteryTicket
//
//  Created by wpapple on 16/5/25.
//  Copyright © 2016年 红红火火恍恍惚惚嘿嘿嘿. All rights reserved.
//

#import "TabBarView.h"


@interface TabBarView()

@property (strong , nonatomic) NSMutableArray * dataArray;
@property (strong , nonatomic) TabBarButton * selectedButton;

@end

@implementation TabBarView

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        NSArray * imageArray = @[@"TabBar_LotteryHall",@"TabBar_Arena",@"TabBar_Discovery",@"TabBar_History",@"TabBar_MyLottery"];
        NSArray * selectedImageArray = @[@"TabBar_LotteryHall_selected",@"TabBar_Arena_selected",@"TabBar_Discovery_selected",@"TabBar_History_selected",@"TabBar_MyLottery_selected"];
        
        for (int i = 0; i < 5; i++) {
            UIImage * image = [UIImage imageNamed:imageArray[i]];
            UIImage * selectedImage = [UIImage imageNamed:selectedImageArray[i]];
            TabBarButton * button = [TabBarButton buttonWithType:UIButtonTypeCustom];
            [button setBackgroundImage:image forState:UIControlStateNormal];
            [button setBackgroundImage:selectedImage forState:UIControlStateSelected];
            
            [button addTarget:self action:@selector(touchTabBarViewButton:) forControlEvents:UIControlEventTouchUpInside];
            
            button.tag = i;
            
            if (i == 0) {
                button.selected = YES;
                self.selectedButton = button;
            }
            
            [self addSubview:button];
            [_dataArray addObject:button];
        }
    }
    return _dataArray;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat margin = (self.frame.size.width - 300) / 6;
    
    for (int i = 0; i < self.dataArray.count; i++) {
        TabBarButton * button = self.dataArray[i];
        button.frame = CGRectMake(margin * (i + 1) + 60 * i, 0, 60, 49);
    }
}

-(void)touchTabBarViewButton:(TabBarButton *)sender{
    self.selectedButton.selected = NO;
    sender.selected = YES;
    self.selectedButton = sender;
    if ([self.delegate respondsToSelector:@selector(tabBarView:didBtnClick:)]) {
        [self.delegate tabBarView:self didBtnClick:sender];
    }
}

@end
